package fy.erp.access;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import fy.util.MyUtil;

/**
 * 单例 获取OA-ERP数据交互，科目段和项目令号关联获得项目使用费用报表
 * 
 * @author Administrator
 * 
 */
public class ProjectExpenseReportsAccessManager {

	private static ProjectExpenseReportsAccessManager perm;

	private List<Long> members;

	private String accessFile;

	private ProjectExpenseReportsAccessManager() {
		String rootPath = MyUtil
				.getRealPath(ProjectExpenseReportsAccessManager.class);
		accessFile = rootPath + "projectExpenseReportsAccess";
		members = new ArrayList<Long>();
		try {
			read();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static ProjectExpenseReportsAccessManager getInstance() {
		if (perm == null) {
			synchronized (ProjectExpenseReportsAccessManager.class) {
				if (perm == null) {
					perm = new ProjectExpenseReportsAccessManager();
				}
			}
		}
		return perm;
	}

	public void newAccess(List<Long> members) throws Exception {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		// MyUtil.
		try {
			fos = new FileOutputStream(accessFile);
			osw = new OutputStreamWriter(fos, "utf-8");

			for (int i = 0, size = members.size(); i < size; i++) {
				osw.write(String.valueOf(members.get(i)));
				osw.write("\n");
			}

		} finally {
			osw.flush();
			osw.close();
			fos.close();
		}

		this.members = members;
	}

	private void read() throws Exception {
		File file = new File(accessFile);
		if (file.isFile()) {
			BufferedReader br = null;
			try {

				br = MyUtil.createBufferedReader(accessFile, "utf-8");
				String temp = null;
				while ((temp = br.readLine()) != null) {
					members.add(Long.valueOf(temp));
				}

			} finally {
				br.close();
			}
		}
	}

	public boolean isArrow(long member) {
		return members != null && members.indexOf(Long.valueOf(member)) != -1;
	}
}
